import { constantRoutes, asyncRoutes } from '@/router/index'
const state = {
  routes: []
}

const mutations = {
  setRoutes(state, data) {
    state.routes = [...constantRoutes, ...data]
  }
}

const actions = {
  filterRoutes(context, menuMark) {
    // asyncRoutes 完整的动态路由
    // menuMark 整理好的动态路由
    const userAsyncRoutes = []
    asyncRoutes.forEach(item => {
      // 一级
      if (menuMark.find(obj => obj.mark === item.name)) {
        if (item.children && item.children.length) {
          const temp = item.children.filter(val => {
            return menuMark.find(o => o.mark === val.name)
          })
          item.children = temp
        }
        userAsyncRoutes.push(item)
      }
    })
    context.commit('setRoutes', userAsyncRoutes)
    return userAsyncRoutes
  }
}

const getters = {}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
